package com.yqRecite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqRecite.entity.Comments;
import com.yqRecite.entity.Dynamic;
import com.yqRecite.entity.Words;
import com.yqRecite.mapper.CommentsMapper;
import com.yqRecite.mapper.DynamicMapper;
import com.yqRecite.service.DynamicService;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import com.yqRecite.vo.DynamicVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DynamicServiceImpl implements DynamicService {
    @Resource
    private DynamicMapper dynamicMapper;
    @Resource
    private CommentsMapper commentsMapper;

    @Override
    public Result addOneDynamic(Dynamic dynamic) {
        int res = dynamicMapper.insert(dynamic);
        if (res == 1){
            System.out.println("动态上传成功, dynamic==>" + dynamic);
            return ResultUtil.ok(dynamic);
        }else {
            return ResultUtil.error("增加dynamic出现异常", dynamic);
        }
    }

    /**
     * 获取所有人发布的动态
     * @return
     */
    @Override
    public Result getAllDynamicList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("createTime");//最新的显示在前面
        List<Dynamic> dynamicList = dynamicMapper.selectList(wrapper);
        List<DynamicVo> dynamicVoList = new ArrayList<>();
        for (Dynamic dynamic: dynamicList) {
            DynamicVo dynamicVo = new DynamicVo();
            dynamicVo.setId(dynamic.getId());
            dynamicVo.setContent(dynamic.getContent());
            dynamicVo.setUid(dynamic.getUid());
            dynamicVo.setAvatar(dynamic.getAvatar());
            dynamicVo.setUsername(dynamic.getUsername());
            dynamicVo.setLikeList(dynamic.getLikeList());
            dynamicVo.setThumbList(dynamic.getThumbList());
            dynamicVo.setLikeNum(dynamic.getLikeNum());
            dynamicVo.setThumbNum(dynamic.getThumbNum());
            dynamicVo.setImgUrls(dynamic.getImgUrls().split(","));
            dynamicVo.setCreateTime(dynamic.getCreateTime());
            dynamicVoList.add(dynamicVo);
        }
        System.out.println("dynamicList==>"+ dynamicVoList);
        return ResultUtil.ok(dynamicVoList);
    }

    /**
     * 获取所有我发布的动态
     * @param uid
     * @return
     */
    @Override
    public Result getMyDynamicList(int uid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uid", uid);
        wrapper.orderByDesc("createTime");//最新的显示在前面
        List<Dynamic> dynamicList = dynamicMapper.selectList(wrapper);
        List<DynamicVo> dynamicVoList = new ArrayList<>();
        for (Dynamic dynamic: dynamicList) {
            DynamicVo dynamicVo = new DynamicVo();
            dynamicVo.setId(dynamic.getId());
            dynamicVo.setContent(dynamic.getContent());
            dynamicVo.setUid(dynamic.getUid());
            dynamicVo.setAvatar(dynamic.getAvatar());
            dynamicVo.setUsername(dynamic.getUsername());
            dynamicVo.setLikeList(dynamic.getLikeList());
            dynamicVo.setThumbList(dynamic.getThumbList());
            dynamicVo.setLikeNum(dynamic.getLikeNum());
            dynamicVo.setThumbNum(dynamic.getThumbNum());
            dynamicVo.setImgUrls(dynamic.getImgUrls().split(","));
            dynamicVo.setCreateTime(dynamic.getCreateTime());
            dynamicVoList.add(dynamicVo);
        }
        System.out.println("dynamicList==>"+ dynamicVoList);
        return ResultUtil.ok(dynamicVoList);
    }

    /***
     * 删除我发布的动态
     * @param id
     * @return
     */
    @Override
    public Result deleteDynamic(int id) {
        Dynamic dynamic = dynamicMapper.selectById(id);
        if (dynamic == null){
            return ResultUtil.error("此动态不存在");
        }else {
            int res = dynamicMapper.deleteById(id);
            if (res == 1){
                System.out.println("成功！");
                return ResultUtil.ok(dynamic);
            }else {
                System.out.println("动态删除失败！");
                return ResultUtil.fail("失败", dynamic);
            }
        }
    }

    /**
     * 用户喜欢了一次动态
     * @param uid 用户id
     * @param id 喜欢的动态id
     * @return
     */
    @Override
    public Result likeOneDynamic(String uid, int id) {
        System.out.println("uid="+uid+", id="+id);
        //1、找到当前动态数据
        Dynamic dynamic = dynamicMapper.selectById(id);
        //2、修改喜欢用户列表
        String[] rawArray = dynamic.getLikeList().split(",");
        // 使用Java 8的流API来过滤空字符串，并收集到List<String>中
        List<String> likeList = Arrays.stream(rawArray)
                .map(String::trim) // 去除每个字符串两端的空白符
                .filter(s -> !s.isEmpty()) // 过滤掉空字符串
                .collect(Collectors.toList()); // 收集到List中
        //2-1加入喜欢列表
        likeList.add(uid);
        //3、重新存入数据到动态表
        dynamic.setLikeList(String.join(",", likeList));
        dynamic.setLikeNum(dynamic.getLikeNum()+1);//数量+1
        System.out.println(dynamic);
        //4、修改
        int res = dynamicMapper.updateById(dynamic);
        if (res == 1){
            return ResultUtil.ok(dynamic);
        }else {
            return ResultUtil.fail("修改失败", dynamic);
        }
    }

    /**
     * 用户取消喜欢动态
     * @param uid 用户id
     * @param id 取消喜欢的动态id
     * @return
     */
    @Override
    public Result cancelLikeOneDynamic(String uid, int id) {
        //1、找到当前动态数据
        Dynamic dynamic = dynamicMapper.selectById(id);
        //2、修改喜欢用户列表
        String[] rawArray = dynamic.getLikeList().split(",");
        // 使用Java 8的流API来过滤空字符串，并收集到List<String>中
        List<String> likeList = Arrays.stream(rawArray)
                .map(String::trim) // 去除每个字符串两端的空白符
                .filter(s -> !s.isEmpty()) // 过滤掉空字符串
                .collect(Collectors.toList()); // 收集到List中
        //2-1移出喜欢列表
        boolean isRemove = likeList.remove(uid);
        if (isRemove){
            System.out.println("移出成功");
        }
        //3、重新存入数据到动态表
        dynamic.setLikeList(String.join(",", likeList));
        dynamic.setLikeNum(dynamic.getLikeNum()-1);//数量-1
        System.out.println(likeList);
        //4、修改
        int res = dynamicMapper.updateById(dynamic);
        if (res == 1){
            return ResultUtil.ok(dynamic);
        }else {
            return ResultUtil.fail("修改失败", dynamic);
        }
    }

    /**
     * 用户点赞了一次动态
     * @param uid 用户id
     * @param id 点赞的动态id
     * @return
     */
    @Override
    public Result thumbOneDynamic(String uid, int id) {
        //1、找到当前动态数据
        Dynamic dynamic = dynamicMapper.selectById(id);
        //2、修改喜欢用户列表
        String[] rawArray = dynamic.getThumbList().split(",");
        // 使用Java 8的流API来过滤空字符串，并收集到List<String>中
        List<String> thumbList = Arrays.stream(rawArray)
                .map(String::trim) // 去除每个字符串两端的空白符
                .filter(s -> !s.isEmpty()) // 过滤掉空字符串
                .collect(Collectors.toList()); // 收集到List中
        //2-1加入喜欢列表
        thumbList.add(uid);
        //3、重新存入数据到动态表
        dynamic.setThumbList(String.join(",", thumbList));
        dynamic.setThumbNum(dynamic.getThumbNum()+1);//数量+1
        System.out.println(dynamic);
        //4、修改
        int res = dynamicMapper.updateById(dynamic);
        if (res == 1){
            return ResultUtil.ok(dynamic);
        }else {
            return ResultUtil.fail("修改失败", dynamic);
        }
    }

    /**
     * 用户取消点赞动态
     * @param uid 用户id
     * @param id 取消点赞的动态id
     * @return
     */
    @Override
    public Result cancelThumbOneDynamic(String uid, int id) {
        //1、找到当前动态数据
        Dynamic dynamic = dynamicMapper.selectById(id);
        //2、修改点赞用户列表
        String[] rawArray = dynamic.getThumbList().split(",");
        // 使用Java 8的流API来过滤空字符串，并收集到List<String>中
        List<String> thumbList = Arrays.stream(rawArray)
                .map(String::trim) // 去除每个字符串两端的空白符
                .filter(s -> !s.isEmpty()) // 过滤掉空字符串
                .collect(Collectors.toList()); // 收集到List中
        //2-1移出点赞列表
        boolean isRemove = thumbList.remove(uid);
        if (isRemove){
            System.out.println("移出成功");
        }
        //3、重新存入数据到动态表
        dynamic.setThumbList(String.join(",", thumbList));
        dynamic.setThumbNum(dynamic.getThumbNum()-1);//数量-1
        //4、修改
        int res = dynamicMapper.updateById(dynamic);
        if (res == 1){
            return ResultUtil.ok(dynamic);
        }else {
            return ResultUtil.fail("修改失败", dynamic);
        }
    }

    /**
     * 进入动态内部
     * @param id
     * @return 动态详情，评论区列表
     */
    @Override
    public Result getDynamicInner(int id) {
        Map<String, Object> returnMap = new HashMap<>();
        Dynamic dynamic = dynamicMapper.selectById(id);
        if (dynamic == null){
            System.out.println("动态为空");
            return ResultUtil.fail("动态信息获取失败", null);
        }
        DynamicVo dynamicVo = new DynamicVo();
        dynamicVo.setId(dynamic.getId());
        dynamicVo.setContent(dynamic.getContent());
        dynamicVo.setUid(dynamic.getUid());
        dynamicVo.setUsername(dynamic.getUsername());
        dynamicVo.setAvatar(dynamic.getAvatar());
        dynamicVo.setLikeList(dynamic.getLikeList());
        dynamicVo.setThumbList(dynamic.getThumbList());
        dynamicVo.setLikeNum(dynamic.getLikeNum());
        dynamicVo.setThumbNum(dynamic.getThumbNum());
        dynamicVo.setImgUrls(dynamic.getImgUrls().split(","));
        dynamicVo.setCreateTime(dynamic.getCreateTime());
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("dynamicId", id);//匹配为动态id
        List<Comments> resC = commentsMapper.selectList(wrapper);
//        List<Comments> resC = commentsMapper.getAllCommentsByDid(id);
        returnMap.put("dynamic", dynamicVo);
        returnMap.put("commentList", resC);
        System.out.println("commentList==>"+resC);
        return ResultUtil.ok(returnMap);
    }
}
